﻿using System.Collections;

namespace CSharpPro._1023;

public class Demo1023
{
    public void Test()
    {
        int[] arr = new int[5];
        arr[0] = 1;
        arr[4] = 5;
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine(arr[i] + "\t");
        }

        String[] arr2 = new String[5];
        arr2[0] = "hello";
        arr2[4] = "world";
        for (int i = 0; i < arr2.Length; i++)
        {
            Console.WriteLine(arr2[i] + "\t");
        }

        int[] arr3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Console.WriteLine(arr3[0] + "\t");

        int[,] arr4 = new int[3, 3];
        arr4[0, 0] = 1;
        arr4[1, 1] = 5;
        arr4[2, 2] = 6;

        int[,] arr5 =
        {
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 7, 8, 9 }
        };

        int[] arr6 = [4, 555, 22, 12, 88];
        Array.Sort(arr6);
        Array.Reverse(arr6); //和Sort连用
        foreach (int i in arr6)
        {
            Console.WriteLine(i + "\t");
        }

        ArrayList list = new ArrayList();
        list.Add("1");
        list.Add("2");
        list.Add("3");
        list.Add("hello");
        list.Add("world");
        Console.WriteLine(list.Count);
        Console.WriteLine();
        foreach (object i in list)
        {
            Console.WriteLine(i);
        }

        list.Contains("hello");

        Hashtable ht = new Hashtable();
        ht.Add("name", "张三");
        ht.Add("age", 18);
        ht.Add("sex", "男");
        foreach (DictionaryEntry i in ht)
        {
            Console.WriteLine(i.Key + "\t" + i.Value);
        }

        ht.Remove("age");
    }

    public static void BubbleSort(int[] array)
    {
        if (array == null || array.Length <= 1)
            return;

        int length = array.Length;
        for (int i = 0; i < length - 1; i++)
        {
            bool swapped = false;
            for (int j = 0; j < length - 1 - i; j++)
            {
                if (array[j] > array[j + 1])
                {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }

            if (!swapped)
                break;
        }
    }
}
    

    